package com.huixuebao.exchange.service.Impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huixuebao.exchange.entity.CaptchaEntity;
import com.huixuebao.exchange.mapper.CaptchaMapper;
import com.huixuebao.exchange.service.CaptchaService;
import com.huixuebao.exchange.util.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    CaptchaMapper captchaMapper;
    /**
     * @Title checkCaptcha
     * @Description:校验验证码
     */
    @Override
    public boolean checkCaptcha(String captchaUid,String captchaType,String captchaValue,String mobileNo){
        String createTime =  DateUtil.formatDateTime(DateUtil.offset(new Date(), DateField.MINUTE,-5));
        CaptchaEntity captchaEntity = new CaptchaEntity();
        captchaEntity.setCaptchaType(captchaType);
        captchaEntity.setCaptchaValue(captchaValue.toUpperCase());
        captchaEntity.setCaptchaUid(captchaUid);
        if(CaptchaUtil.CAPTCHA_TYPE_MOBILE.equals(captchaType)){
            captchaEntity.setPhone(mobileNo);
        }
        QueryWrapper<CaptchaEntity> queryWrapper = new QueryWrapper<>(captchaEntity);
        queryWrapper.gt("create_time",createTime);
        CaptchaEntity captcha = this.captchaMapper.selectOne(queryWrapper);
        if(captcha!=null){
            //验证码校验之后进行删除，保证验证码只能使用一次(暂时屏蔽-0401)
            //this.removeById(captcha);
            return true;
        }else{
            return false;
        }
    }
}
